<?

define("_1ST"                  , 0);
define("_2ND"                  , 1);
define("_DEFAULT_INCREMENT"    , 1);

/**
* These are what we need
* Warehouse   => where the item goes to
* Ser-Number  => we do string extraction and create
*                each model entry in inventory history
*
*/
fetchWarehouse($COracle,
               $thisJobId,
               $srcDbHdr,
               $warehouse);

if($warehouse != " "){

  $dstDbInventory = "WHS_" . $warehouse;
}else{

  usrDie("Error: Invalid Warehouse's Name");
}

fetchNDetail($COracle,
             $thisJobId,
             $srcDbDtl,
             $nDetail,
             $noDetail,
             $lstDetail);

for($iDtl = 0; $iDtl < $nDetail; $iDtl++){

  /**
  * thisJobId will no longer takes
  * apprThisJobId since is current
  * selected Tx's header job id
  *
  * recvId = $lstDetail["RECV_REF"]
  * reflects the source of this tx
  * thus we could process the JobId of details
  * to manage the history and inventory
  *
  */

  //$recvId = $lstDetail["RECV_REF"][$iDtl];

  fetchNSubDetail($COracle,
                  $thisJobId,
                  $noDetail[$iDtl],
                  $srcDbSubDtl,
                  $nSub,
                  $subDetail);

  for($iSub = 0; $iSub < $nSub; $iSub++){

    /**
    * Extract the serial number
    * if serial number is exists
    * YES: Process model history entry
    * NO : process model without serNumber
    *
    */
    if($subDetail["SER_NUMBER"][$iSub] != ""){

      $serNumber = explode(_DEFAULT_SEPARATOR_02,
                           $subDetail["SER_NUMBER"][$iSub]);

      /**
      * Check if serNumber[_1ST] is not _MANUAL_KEY_IN
      *
      * YES: it means order range
      * NO : it means manual key in
      *
      */
      if($serNumber[_1ST] != _MANUAL_KEY_IN){

        /**
        * Fetch the head and tail
        *
        */
        procSerNumRealValue($subDetail["HEAD"][$iSub],
                            $subDetail["TAIL"][$iSub],
                            $serNumber[_2ND],
                            $serNumber[_1ST],
                            $theRange);

        if($theRange < 0){

          usrDie("Error: Serial number range is invalid <br/> ".
                 "Reason-> Negative value on _2ND ");
        }

        /**
        * for each ser number within range
        * Do -> insert into inventory and history
        *
        */
        for($iSer = 0; $iSer < $theRange; $iSer++){

          procSerNumber($subDetail["HEAD"][$iSub],
                        $subDetail["TAIL"][$iSub],
                        $serNumber[_1ST],
                        $iSer,
                        $theSerial);

          insToInventory($COracle,
                         $subDetail["NO"][$iSub],
                         $subDetail["MODEL"][$iSub],
                         $subDetail["MODEL_ID"][$iSub],
                         $subDetail["UNIT"][$iSub],
                         _DEFAULT_INCREMENT,
                         $dstDbInventory);

          fetchLastNoHistory($COracle,
                             $subDetail["MODEL"][$iSub],
                             $subDetail["MODEL_ID"][$iSub],
                             $subDetail["UNIT"][$iSub],
                             $iSer,
                             $dstDbHistory,
                             $lastNumber);

          /**
          * Full listing Type of Tx can be found at
          *
          * @see ORACLE_DB.ECS_APPL.MST_TYPE_TX
          *
          */
          $txType   = _PEMBELIAN;

          $trDate   = " TO_DATE('".$srcDate."', 'dd-mm-YYYY')";

          $values = "  ".$lastNumber." ,                    ".
                    "  ".$trDate." ,                        ".
                    //" '".$recvId."',                        ".
                    " '".$thisJobId."',                        ".
                    "  ".$subDetail["NO"][$iSub]." ,        ".
                    " '".$subDetail["MODEL"][$iSub]."',     ".
                    "  ".$subDetail["MODEL_ID"][$iSub]." ,  ".
                    " '".$subDetail["UNIT"][$iSub]."',      ".
                    " '".$theSerial."'                      ";

          insToHistory($COracle,
                       $values,
                       $txType,
                       $dstDbHistory);
        }
      }else{

        $manualJobId = $serNumber[_2ND];

        /**
        * Extract the manual key in
        * And insert them as like source order like
        *
        */
        fetchManualKeyInJobIdData($COracle,
                                  $manualJobId,
                                  $srcDbManInDtl,
                                  $mnlRows,
                                  $mnlResult);

        for($iRow = 0; $iRow < $mnlRows; $iRow++){

          insToInventory($COracle,
                         $subDetail["NO"][$iSub],
                         $subDetail["MODEL"][$iSub],
                         $subDetail["MODEL_ID"][$iSub],
                         $subDetail["UNIT"][$iSub],
                         _DEFAULT_INCREMENT,
                         $dstDbInventory);

          fetchLastNoHistory($COracle,
                             $subDetail["MODEL"][$iSub],
                             $subDetail["MODEL_ID"][$iSub],
                             $subDetail["UNIT"][$iSub],
                             $mnlResult["SER_NUMBER"][$iRow],
                             $dstDbHistory,
                             $lastNumber);

          /**
          * Full listing Type of Tx can be found at
          *
          * @see ORACLE_DB.ECS_APPL.MST_TYPE_TX
          *
          */
          $txType   = _PEMBELIAN;

          $trDate   = " TO_DATE('".$srcDate."', 'dd-mm-YYYY')";

          $values = "  ".$lastNumber." ,                    ".
                    "  ".$trDate." ,                        ".
                    //" '".$recvId."',                        ".
                    " '".$thisJobId."',                        ".
                    "  ".$subDetail["NO"][$iSub]." ,        ".
                    " '".$subDetail["MODEL"][$iSub]."',     ".
                    "  ".$subDetail["MODEL_ID"][$iSub]." ,  ".
                    " '".$subDetail["UNIT"][$iSub]."',      ".
                    " '".$mnlResult["SER_NUMBER"][$iRow]."' ";

          insToHistory($COracle,
                       $values,
                       $txType,
                       $dstDbHistory);
        }
      }
    }else{

      /**
      * This procedure will be suspended
      * and will be replaced by insTxHistory()
      *
      */
      //$theRange  = $subDetail["RECV_QTY"][$iSub];

      /**
      * for each ser number within range
      * Do -> insert into inventory and history
      *
      */
      /*
      for($iSer = 0; $iSer < $theRange; $iSer++){

        $serNumber = _NO_NUMBER ." ". $iSer;

        insToInventory($COracle,
                       $subDetail["NO"][$iSub],
                       $subDetail["MODEL"][$iSub],
                       $subDetail["MODEL_ID"][$iSub],
                       $subDetail["UNIT"][$iSub],
                       _DEFAULT_INCREMENT,
                       $dstDbInventory);

        fetchLastNoHistory($COracle,
                           $subDetail["MODEL"][$iSub],
                           $subDetail["MODEL_ID"][$iSub],
                           $subDetail["UNIT"][$iSub],
                           $serNumber,
                           $dstDbHistory,
                           $lastNumber);
        /**
        * Full listing Type of Tx can be found at
        *
        * @see ORACLE_DB.ECS_APPL.MST_TYPE_TX
        *
        */
        /*
        $txType   = _PEMBELIAN;

        $trDate   = " TO_DATE('".$srcDate."', 'dd-mm-YYYY')";

        $values = "  ".$lastNumber." ,                    ".
                  "  ".$trDate." ,                        ".
                  " '".$recvId."',                     ".
                  "  ".$subDetail["NO"][$iSub]." ,        ".
                  " '".$subDetail["MODEL"][$iSub]."',     ".
                  "  ".$subDetail["MODEL_ID"][$iSub]." ,  ".
                  " '".$subDetail["UNIT"][$iSub]."',      ".
                  " '".$serNumber."'                      ";

        insToHistory($COracle,
                     $values,
                     $txType,
                     $dstDbHistory);
      }*/
    }
  }
}

?>